package com.ksyun.ks3.services.handler;

import com.ksyun.ks3.exception.Ks3ClientException;
import com.ksyun.ks3.exception.Ks3ServiceException;
import com.ksyun.ks3.model.BucketQuota;
import com.ksyun.ks3.model.result.GetBucketQuotaResult;
import com.ksyun.ks3.services.request.GetBucketQuotaRequest;
import com.ksyun.ks3.services.request.Ks3HttpRequest;
import com.ksyun.ks3.util.ResponseUtils;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import okhttp3.Headers;

public abstract class GetBucketQuotaResponseHandler extends Ks3HttpResponseHandler {

    public abstract void onFailure(GetBucketQuotaRequest request, Ks3ClientException clientException, Ks3ServiceException serviceException);

    public abstract void onSuccess(GetBucketQuotaRequest request, GetBucketQuotaResult result);

    @Override
    public final void onSuccess(Ks3HttpRequest request, int statusCode, Headers responseHeaders, byte[] response) {
        GetBucketQuotaResult result = parseXML(response);
        result.setStatusCode(statusCode);
        result.setHeaders(ResponseUtils.parseResponseHeaders(responseHeaders));
        this.onSuccess((GetBucketQuotaRequest) request, result);
    }

    @Override
    public final void onFailure(Ks3HttpRequest request, Ks3ClientException clientException, Ks3ServiceException serviceException) {
        this.onFailure((GetBucketQuotaRequest) request, clientException, serviceException);
    }

    @Override
    public final void onProgress(long bytesWritten, long totalSize) {
    }

    @Override
    public final void onStart() {
    }

    @Override
    public final void onFinish() {
    }

    @Override
    public final void onCancel() {
    }

    private GetBucketQuotaResult parseXML(byte[] response) {
        GetBucketQuotaResult result = new GetBucketQuotaResult();
        XmlPullParserFactory factory;
        BucketQuota quota = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            XmlPullParser parse = factory.newPullParser();
            parse.setInput(new ByteArrayInputStream(response), "UTF-8");
            int eventType = parse.getEventType();
            while (XmlPullParser.END_DOCUMENT != eventType) {
                String nodeName = parse.getName();
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.END_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        if ("Quota".equalsIgnoreCase(nodeName)) {
                            quota = new BucketQuota();
                        }
                        if ("StorageQuota".equalsIgnoreCase(nodeName)) {
                            quota.setStorageQuota(Long.parseLong(parse.nextText()));
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (nodeName.equalsIgnoreCase("Quota")) {

                        }
                        break;
                    case XmlPullParser.TEXT:

                        break;
                    default:
                        break;
                }
                eventType = parse.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setBucketQuota(quota);
        return result;
    }
}